﻿using strange.extensions.context.api;
using strange.extensions.dispatcher.eventdispatcher.api;
using strange.extensions.mediation.impl;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace CommonIoC
{

    public enum TouchButtonEvent
    {
        
        ClickRightDown

    }
    public class MainUIMediator : Mediator
    {

       

        [Inject]
        public MainUIView view { get; set; }
        [Inject(ContextKeys.CONTEXT_DISPATCHER)]//全局的dispatcher
        public IEventDispatcher dispatcher { get; set; }

        public override void OnRegister()
        {
            //注册监听，，controller分发的事件，，
            //dispatcher.AddListener(PlayerStageCommandEvent.canMove, onPlayerStageChangeTocanMove);
            dispatcher.AddListener(PlayerGameState.IsDoingTalk, onIsDoingTalk);
            dispatcher.AddListener(PlayerGameState.CanMove_And_CanTouchWSAD, onCanMove_And_CanTouchWSAD);

            dispatcher.AddListener(  TopButtonListEvent.ClickBack , onTopButtonListClickBack);


            AddLeftMenuButtonClickFunc();
            AddRightQuitButtonClickFunc();
            AddRightDownButtonClickFunc();

        }

        private void onTopButtonListClickBack(IEvent payload)
        {
            view.ShowThisUI();
        }

        private void onCanMove_And_CanTouchWSAD(IEvent payload)
        {
            //view.ChangeStageTocanMove();
            view.ShowThisUI();
        }

        private void onIsDoingTalk(IEvent payload)
        {
            // view.ChangeStageToDoTalk();
            view.CloseThisUI();
        }

        void AddLeftMenuButtonClickFunc()
        {
            if(view.btn_left_menu != null)
            {
                view.btn_left_menu.onClick.AddListener(delegate ()
                {

                    dispatcher.Dispatch( MainUITouchEvnet.OpenTopMenuButtonList );
                    view.CloseThisUI();
                });
            }
        }

        void AddRightQuitButtonClickFunc()
        {
            if (view.btn_right_quit != null)
            {
                view.btn_right_quit.onClick.AddListener(delegate ()
                {

                    //dispatcher.Dispatch(MainUITouchEvnet.OpenTopMenuButtonList);
                    SceneManager.LoadScene(0);
                    view.CloseThisUI();
                });
            }
        }

        void AddRightDownButtonClickFunc()
        {
            if (view.btn_right_down != null)
            {
                view.btn_right_down.onClick.AddListener(delegate ()
                {

                    
                    try
                    {
                        GameObject.Find("GameMainPlayer").GetComponent<TalkPressF>().Check_Call_Talk();
                        // GameDataManager.Instance.SetPlayerToDriveTank();

                    }
                    catch
                    {
                        Debug.LogError("当前场景里面没有GameMainPlayer对象");
                    }
                    //dispatcher.Dispatch(MainUITouchEvnet.OpenTopMenuButtonList);
                    // SceneManager.LoadScene(0);
                    // view.CloseThisUI();
                });
            }
        }


        public override void OnRemove()
        {
            dispatcher.RemoveListener(PlayerGameState.IsDoingTalk, onIsDoingTalk);
            dispatcher.RemoveListener(PlayerGameState.CanMove_And_CanTouchWSAD, onCanMove_And_CanTouchWSAD);


        }


    }
}
